package mall.repository;

import mall.entity.Image;

import java.sql.ResultSet;
import java.util.List;

public class ImageDao extends BaseDao<Image> {
    public int insert(Image image) {
        String sql = "insert into image(id, data) values(?, ?)";
        return executeUpdate(sql, image.getId(), image.getData());
    }

    public Image selectById(String id) {
        String sql = "select * from image where id = ?";
        List<Image> list = executeQuery(sql, id);
        return list.isEmpty() ? null : list.get(0);
    }

    @Override
    public Image rowToObject(ResultSet rs) throws Exception {
        return new Image(rs.getString("id"),
                rs.getBlob("data").getBinaryStream());
    }


}
